home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4754 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  148 lines

  1. Path: news.ifm.liu.se!d93ricka
  2. From: d93ricka@isy.liu.se (Richard Karlsson)
  3. Newsgroups: comp.lang.c
  4. Subject: Help! Can't wait for two files with aiowait.
  5. Date: 6 Feb 1996 17:03:52 GMT
  6. Message-ID: <4f81lo$not@newsy.ifm.liu.se>
  7. NNTP-Posting-Host: idun.isy.liu.se
  8. Mime-Version: 1.0
  9. Content-Type: text/plain; charset=iso-8859-1
  10. Content-Transfer-Encoding: 8bit
  11. Summary: Aiowait does not return even though outstanding requesta are finished
  12. Keywords: aiowait, aioread, asynchronous
  13. X-Newsreader: NN version 6.5.0 #26 (NOV)
  14.  
  15. Hello
  16.  
  17. I'm trying to wait for more than one file at the same time, with the
  18. aiowait call under SunOS or Solaris. The code below illustrates what
  19. I'm trying to do. My problem is that aiowait does not return even
  20. though one of the outstaning ioread-requests have finished.
  21.  
  22. The program will try to read from "testfil1" and "testfil2" and printf
  23. the result. Do "mkfifo testfil1", "mkfifo testfil2" and open 3
  24. shells. In shell 1 do "cat >>testfil1". In shell 2 do "cat
  25. >>testfil2". And in shell 3 do "testaiowait". Now write
  26. "111111111111111111111111111111111111" into shell 1 and press return
  27. in shell 2 a few times. You will notice that only 4 "1":s are read
  28. from shell 1 for every return you press in shell 2.
  29.  
  30. Please help me understan why.
  31.  
  32. (Remembler to link with aio (option -laio to the linker (compiler)))
  33.  
  34. // testaiowait.c
  35.  
  36. #include <stdio.h>
  37. #include <sys/asynch.h>
  38. #include <sys/time.h>
  39. #include <sys/types.h>
  40. #include <unistd.h>
  41.  
  42. #define RETURNCODE_FATAL 20
  43. #define RETURNCODE_OK 0
  44.  
  45. #define TRUE 1
  46. #define FALSE 0
  47.  
  48. main()
  49. {
  50.     FILE *fil1, *fil2;
  51.     aio_result_t aioresult1, aioresult2, *aioresult;
  52.     char buffer1[1000], buffer2[1000];
  53.     int res1free=TRUE, res2free=1;
  54.     int eof1=FALSE, eof2=FALSE;
  55.     
  56.     if((fil1=fopen("testfil1","r"))==0)
  57.     {
  58.     perror("Can't open testfil1");
  59.     exit(RETURNCODE_FATAL);
  60.     }
  61.     
  62.     if((fil2=fopen("testfil2","r"))==0)
  63.     {
  64.     perror("Can't open testfil2");
  65.     exit(RETURNCODE_FATAL);
  66.     }
  67.  
  68.     do
  69.     {
  70.     if((res1free)&&!eof1) /* If aio_result free and not at eof do another read */
  71.     {
  72.         aioresult1.aio_errno=AIO_INPROGRESS;
  73.         res1free=FALSE;
  74.         if(aioread(fileno(fil1), buffer1, 1, 0, SEEK_END, &aioresult1)==-1)
  75.         {
  76.         perror("Can't aioread on testfil1");
  77.         exit(RETURNCODE_FATAL);
  78.         }
  79.     }
  80.  
  81.     if(res2free&&!eof2) /* If aio_result free and not at eof do another read */
  82.     {
  83.         aioresult2.aio_errno=AIO_INPROGRESS;
  84.         res2free=FALSE;
  85.         if(aioread(fileno(fil2), buffer2, 1, 0, SEEK_END, &aioresult2)==-1)
  86.         {
  87.         perror("Can't aioread on testfil2");
  88.         exit(RETURNCODE_FATAL);
  89.         }
  90.     }
  91.  
  92.     if((int)(aioresult=aiowait(0))==-1) /* aiowait */
  93.     {
  94.         perror("Can't aiowait");
  95.         exit(RETURNCODE_FATAL);
  96.     };
  97.  
  98.     if(aioresult==&aioresult1) /* if read on testfil1 was finished do... */
  99.     {
  100.         res1free=TRUE;
  101.         if(aioresult->aio_return==0)
  102.           {
  103.           eof1=TRUE;
  104.           } 
  105.         else
  106.           {
  107. /* This is necessary if file is seekable.
  108.           if(lseek(fileno(fil1), 1, SEEK_CUR)==-1)
  109.             {
  110.             perror("Can't seek on testfil1");
  111.             exit(RETURNCODE_FATAL);
  112.             }
  113. */
  114.           buffer1[1]='\0';
  115.           printf("%s",buffer1);
  116.           }
  117.     }
  118.     if(aioresult==&aioresult2) /* if read on testfil2 was finished do... */
  119.     {
  120.         res2free=TRUE;
  121.         if(aioresult->aio_return==0)
  122.           {
  123.           eof2=TRUE;
  124.           } 
  125.         else
  126.           {
  127. /* This is necessary if file is seekable.
  128.           if(lseek(fileno(fil2), 1 ,SEEK_CUR)==-1)
  129.             {
  130.             perror("Can't seek on testfil2");
  131.             exit(RETURNCODE_FATAL);
  132.             }
  133. */
  134.           buffer2[1]='\0';
  135.           printf("%s",buffer2);
  136.           }
  137.     }
  138.     } while(!eof1||!eof2);
  139.  
  140.     exit(RETURNCODE_OK);
  141. }
  142.  
  143. --
  144.  
  145. /-------------------------------------------------------------------------+
  146. | Richard Karlsson                          | Rydsvagen 252 C.23          |
  147. | Studying Computer Science and Engineering | S-582 51 Linkoping / Sweden |
  148.